package com.wwdeng.smart.video.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * android
 *
 * @Author：wwdeng
 * @CreateTime：2024/9/17 14:28
 * <pre>
 *
 * </pre>
 */
public class ImageTouchView extends ImageView {

    private float startX;
    private float startY;
    private ImageTouchListener mListener = null;

    public ImageTouchView(Context context) {
        super(context);
    }

    public ImageTouchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                // Handle long press here
                float distanceX = event.getX() - startX;
                float distanceY = event.getY() - startY;
                if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > 30) {
                    if (distanceX > 0) {
                        if (mListener != null){
                            mListener.onSwipeRight();
                        }
                    } else {
                        if (mListener != null){
                            mListener.onSwipeLeft();
                        }
                    }
                } else if (Math.abs(distanceY) > 30) {
                    if (distanceY > 0) {
                        if (mListener != null){
                            mListener.onSwipeBottom();
                        }
                    } else {
                        if (mListener != null){
                            mListener.onSwipeTop();
                        }
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
                if (mListener != null){
                    mListener.onSwipeTouchUp();
                }
                break;
        }
        return true;
    }

    public void setTouchListener(ImageTouchListener listener){
        this.mListener = listener;
    }

    public interface ImageTouchListener {
        void onSwipeRight();
        void onSwipeLeft();
        void onSwipeTop();
        void onSwipeBottom();
        void onSwipeTouchUp();
    }
}
